import React, { useEffect, useState } from 'react'
import {useNavigate}from 'react-router-dom'
import {Button, Space, Table}from 'antd'
import baseUrl from '../axios/config'
import colunm from '../configure/Menu'
import './Home.scss'
import { userInfo } from 'os'


export default function Home() {
  const [ipcRendererdata,setipcRenderdata]=useState('')
  const naviagate = useNavigate()


  //初始化数据
  const [Data ,setData]= useState([])
  const token = localStorage.getItem('token')
  const userinfo = localStorage.getItem('userinfo')
  // 擦除ipc数据
  if(ipcRendererdata !==""){
    setInterval(()=>{
      setipcRenderdata("")
    },5000)
  }
// 通过xiaolu/query后台接口 获取数据然后渲染出来
  useEffect(() =>{
     const fetdata = baseUrl.get(`/xiaolu/query/${userinfo}`).then(res=>setData(res.data))


  })

// 节点检测  账号登录成功之后 发送  运行c盘下的1.bat文件 后台运行
  useEffect(() => {


      // const data = " tasklist|find /i "Pi Network.exe""

      window.electron.ipcRenderer.sendMessage('checknodepi',"run  bat file");
      window.electron.ipcRenderer.once('checknodepi', (arg) => {
              // eslint-disable-next-line no-console
              console.log(arg);

            });




},[])

useEffect(() => {
const LoginName= localStorage.getItem('userinfo')
window.electron.ipcRenderer.sendMessage('userinfo',LoginName)

},[])

  //切换用户
  const userhandover =()=>{
   localStorage.removeItem('token')
   localStorage.removeItem('userinfo')
   naviagate('/')
  }
  // 后台更新docker容器
  useEffect(()=>{
 const setfetdata =  setInterval(()=>{   baseUrl.get(`/xiaolu/queryname/${userinfo}`).
    then(res=>{


      console.log(res.data)
      const data = res.data
      for (let k in data){
       const switc = data[k].switc
       console.log(switc)
       //后台推送重启容器
       if(switc==="1"){
        window.electron.ipcRenderer.sendMessage('docker-restart',"docker restart pi-consensus");
        window.electron.ipcRenderer.once('docker-restart', (arg) => {
          // eslint-disable-next-line no-console
       console.log( arg)

            baseUrl.post("/xiaolu/ned",{
              name:userinfo,
              switc:"0"
            }).then(res=>{console.log(res.data)})


        });
       }
       //  后台推送 抓取客户端本地高度
       if(switc==="2"){
        const loginname = localStorage.getItem('userinfo')
        window.electron.ipcRenderer.sendMessage('fetchlend',loginname);
      //   window.electron.ipcRenderer.once('docker-restart', (arg) => {
      //     // eslint-disable-next-line no-console
      //  console.log( arg)

      //       baseUrl.post("/xiaolu/ned",{
      //         name:userinfo,
      //         switc:"0"
      //       }).then(res=>{console.log(res.data)})


      //   });
       }

       //后台推送， 电脑截图
       if(switc==="3"){
        const loginname = localStorage.getItem('userinfo')
        window.electron.ipcRenderer.sendMessage('jietu',loginname);
      //   window.electron.ipcRenderer.once('docker-restart', (arg) => {
      //     // eslint-disable-next-line no-console
      //  console.log( arg)

      //       baseUrl.post("/xiaolu/ned",{
      //         name:userinfo,
      //         switc:"0"
      //       }).then(res=>{console.log(res.data)})


      //   });
       }
      }



    })

  },20000)


    return ()=>{
      clearInterval(setfetdata)
    }
  },[])



  // 重启容器
  const restart =()=>{
    window.electron.ipcRenderer.sendMessage('docker-restart',"docker restart pi-consensus");
    window.electron.ipcRenderer.once('docker-restart', (arg) => {
      // eslint-disable-next-line no-console
      console.log(arg);
      setipcRenderdata(arg)
    });


  }
  // 清理区块
  const deleteblock =()=>{

  }
  const jietu = ()=>{
    const loginname = localStorage.getItem('userinfo')
    window.electron.ipcRenderer.sendMessage('jietu',loginname);
    // window.electron.ipcRenderer.once('jietu', (arg) => {
    //   // eslint-disable-next-line no-console
    //   console.log(arg);
    //   setipcRenderdata(arg)
    // });
  }
  return (
    <div>
      <h1>{ipcRendererdata}</h1>

      <Table dataSource={Data} columns={colunm}></Table>
      <Space>
      <Button  onClick={userhandover}> 切换用户</Button>

      <Button type="primary" onClick={restart}>重启容器</Button>
      <Button type="primary" onClick={jietu}>截图</Button>
      </Space>

    </div>
  )
}
